(function () {
    angular.module('app')
        .component('xxQuestion', {
            templateUrl: '/app/user/question/question.html',
            controller: ctrl
        });

    function ctrl($scope,$state,$mdDialog,userService,QuestionFactory,urlCnst) {
        /**
         * purpose:http post
         */
        $scope.question = {
            'title':'',
            'sum':'',
            'body':'',
            'consultantId':'',
            'reward':20
        };

        $scope.selectedConsultant = [];
        // get selected consultant
        $scope.toggle = function (username, selected) {
            var idx = selected.indexOf(username);
            if (idx > -1) {
                selected.splice(idx, 1);
            }
            else {
                selected.push(username);
            }
        };

        $scope.postQuestion =function (body) {
            //TODO set consultantId
            body.userId = userService['username'];
            body.consultantId = $scope.selectedConsultant[0];
            // if (asset < $scope.question['reward']) {
            //     urlCnst.notice("抱歉，您的知识币余额不足");
            //     return;
            // }
            if(!body||!body['title']||!body['sum']||!body['body']){
                urlCnst.notice("请将问题输入完整");
                return;
            }
            if(!body['consultantId']){
                urlCnst.notice("请选择咨询师");
                return;
            }
            function cb(err,res) {
                console.log(res['err'],res);
                if (!res['err']) urlCnst.notice("提问成功");
                else {
                    urlCnst.notice("抱歉，您的知识币余额不足");
                    // return;
                }
            }
            QuestionFactory.postQuestion(body,cb);
        };

        $scope.consultantList = [];
        function getConsultantList() {
            function cb(err,res) {
                if(!err)$scope.consultantList = res;
                //console.log(err,res);
            }
            QuestionFactory.getConsultantList(cb);
        }
        getConsultantList();
    }
})()
